﻿using log4net.Config;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using ZK.Common;

namespace ZK.CoalYardTransManage
{
    static class Program
    {
        public static Mutex mutex;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>    
        [STAThread]
        static void Main()
        {
            mutex = new Mutex(true, "OnlyRun");
            if (mutex.WaitOne(0, false))
            {
                try
                {
                    XmlConfigurator.ConfigureAndWatch(new FileInfo("log4net.config"));
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    //处理未捕获的异常
                    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                    //处理UI线程异常
                    //Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                    //处理非UI线程异常
                    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                    //打开某一个窗口
                    Application.Run(new MainForm());


                    //接口测试
                    //Application.Run(new WeightConfirm(new Model.WeightConfirmDto())); 
                    //Application.Run(new RemovalAndWeigh()); 
                    //Application.Run(new OpenClose());
                    // Application.Run(new Test.接口测试());
                    //Application.Run(new Test.Led硬件单测());
                    //Application.Run(new Test.地磅测试());
                    //Application.Run(new Test.摄像头测试());
                    //Application.Run(new Test.led彩色显示屏());
                    //Application.Run(new Test.FormOne());
                    // Application.Run(new Test.杂项测试());
                    //Application.Run(new Test.打印机打印测试());

                }
                catch (Exception ex)
                {
                    Common.Log4.LogManage.WriteErr(ex.ToString());
                }
            }
            else
            {
                IntPtr _handler = (IntPtr)Properties.Settings.Default.Window;
                ShowFormTop(_handler);

                //MessageBox.Show(null, "运销系统CS客户端已经在运行!\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //// 提示信息，可以删除。   
                //Application.Exit();//退出程序   
            }
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Common.Log4.LogManage.WriteErr(e.ExceptionObject.ToString());
            Program.mutex.Close();
            Application.Restart();
        }



        /// <summary>
        /// 显示正常窗体
        /// </summary>
        private const int SW_NORMAL = 1;
        /// <summary>
        /// 还原窗体
        /// </summary>
        private const int SW_RESTORE = 9;

        /// <summary>
        /// 设置由不同线程产生的窗口的显示状态;
        /// 该函数设置由不同线程产生的窗口的显示状态;
        /// 如果函数原来可见，返回值为非零；如果函数原来被隐藏，返回值为零。
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="nCmdShow"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

        /// <summary>
        /// 创建指定窗口的线程设置到前台，并且激活该窗口。
        /// 该函数将创建指定窗口的线程设置到前台，并且激活该窗口。
        /// 键盘输入转向该窗口，并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
        /// 如果窗口设入了前台，返回值为非零；如果窗口未被设入前台，返回值为零。
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        /// <summary>
        /// 判断指定窗体是否已经最小化
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern bool IsIconic(IntPtr hWnd);
        /// <summary>
        /// 判断指定窗体是否已经最大化
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern bool IsZoomed(IntPtr hWnd);

        /// <summary>
        /// 获取当前系统中被激活的窗体
        /// </summary>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        /// <summary>
        /// 获取指定窗口的进程 ID 或线程 ID
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="ProcessId"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

        /// <summary>
        /// 使指定窗体置于最前端显示
        /// </summary>
        /// <param name="winHandler"></param>
        private static void ShowFormTop(IntPtr winHandler)
        {
            IntPtr _handler = winHandler;
            SetForegroundWindow(_handler);
            if (IsIconic(_handler))
            {//最小化时恢复窗体到前端
                ShowWindowAsync(_handler, SW_RESTORE);
            }
            else
            {//隐藏时显示窗体
                ShowWindowAsync(_handler, SW_NORMAL);
            }
        }
    }
}
